package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        // 拷贝到Setmeal
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 插入到setmeal表
        setmealMapper.insert(setmeal);
        // 获取套餐id
        Long setmealId = setmeal.getId();
        // 获取菜品表数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(item -> item.setSetmealId(setmealId));
        // 插入数据到setmealDishMapper表
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 分页查询
     *
     * @param setmealDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealDTO) {
        // 进行分页
        PageHelper.startPage(setmealDTO.getPage(), setmealDTO.getPageSize());
        // 获取数据
        Page<SetmealVO> page = setmealMapper.page(setmealDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 套餐起售停售
     *
     * @param setmeal
     */
    @Override
    public void updateStatus(Setmeal setmeal) {
        setmealMapper.update(setmeal);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SetmealDTO setmealDTO) {
        // 创建对象
        Setmeal setmeal = new Setmeal();
        // 拷贝到对象里面
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 修改套餐
        setmealMapper.update(setmeal);
        // 获取id
        Long setmealId = setmeal.getId();
        // 删除每一个套餐里面的菜品
        setmealDishMapper.deleteById(setmealId);
        // 在进行添加套餐里面的菜品
        // 获取菜品表数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(item -> item.setSetmealId(setmealId));
        // 插入数据到setmealDishMapper表
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = setmealMapper.selectById(id);
        // 获取套餐中的菜品
        List<SetmealDish> setmealDishList = setmealDishMapper.getDishById(id);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        // 启售状态下不能删除
        for (Long id : ids) {
            SetmealVO setmealVO = setmealMapper.selectById(id);
            if (setmealVO.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 删除图片
        File file = new File("C:\\Users\\14467\\Desktop\\java\\code\\sky-take-out\\sky-server\\src\\main\\resources\\images");
        File[] files = file.listFiles();
        // 创建文件名称集合
        List<String> fileList = new ArrayList<>();
        for (int i = 0; i < Objects.requireNonNull(files).length; i++) {
            String fileName = "/" + Objects.requireNonNull(files)[i].getName();
            fileList.add(fileName);
        }
        for (Long id : ids) {
            SetmealVO setmealVO = setmealMapper.selectById(id);
            // 获取文件名称
            String imageName = setmealVO.getImage().substring(setmealVO.getImage().lastIndexOf("/"));
            if (fileList.contains(imageName)) {
                // 删除图片
                File deleteFile = new File("C:\\Users\\14467\\Desktop\\java\\code\\sky-take-out\\sky-server\\src\\main\\resources\\images\\" + imageName);
                deleteFile.delete();
            }
        }
        // 删除套餐
        setmealMapper.deleteBatch(ids);
        // 删除套餐中的菜品
        for (Long id : ids) {
            setmealDishMapper.deleteById(id);
        }
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> list(Long categoryId) {
        return setmealMapper.list(categoryId);
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishBySetmealId(Long id) {
        return setmealDishMapper.getDishBySetmealId(id);
    }
}
